

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/dzx-blog/img/fluid.png">
  <link rel="icon" href="/dzx-blog/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="微信支付JSAPI支付(公众号支付)特点:  用户通过消息或者扫描二维码再微信内部打开网页,可以调用微信支付完成下单购买的流程(公众号) 只能从微信浏览器发起,可以用微信浏览器的内置方法,不需要引用 Js-sdk  所需:  一个可以访问的域名(www.xxx.com) 一个公众号  流程:步骤一:	添加开发者,微信授权登录配置 添加开发者: 在公众号管理后台登录,选择开发者工具—-web开发者工">
<meta property="og:type" content="article">
<meta property="og:title" content="移动端开发">
<meta property="og:url" content="https://deng-zixiong.gitee.io/dzx-blog/2021/10/01/%E7%A7%BB%E5%8A%A8%E7%AB%AF%E5%BC%80%E5%8F%91/index.html">
<meta property="og:site_name" content="猛男邓子雄的博客">
<meta property="og:description" content="微信支付JSAPI支付(公众号支付)特点:  用户通过消息或者扫描二维码再微信内部打开网页,可以调用微信支付完成下单购买的流程(公众号) 只能从微信浏览器发起,可以用微信浏览器的内置方法,不需要引用 Js-sdk  所需:  一个可以访问的域名(www.xxx.com) 一个公众号  流程:步骤一:	添加开发者,微信授权登录配置 添加开发者: 在公众号管理后台登录,选择开发者工具—-web开发者工">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-09-30T16:00:00.000Z">
<meta property="article:modified_time" content="2023-07-19T05:25:41.287Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary_large_image">
  
  
  
  <title>移动端开发 - 猛男邓子雄的博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/dzx-blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/dzx-blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/dzx-blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"deng-zixiong.gitee.io","root":"/dzx-blog/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/dzx-blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/dzx-blog/js/utils.js" ></script>
  <script  src="/dzx-blog/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/dzx-blog/">
      <strong>邓子雄的博客</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/dzx-blog/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/dzx-blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/dzx-blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/dzx-blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/dzx-blog/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/dzx-blog/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="移动端开发"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2021-10-01 00:00" pubdate>
          2021年10月1日 凌晨
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          6.9k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          58 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">移动端开发</h1>
            
            
              <div class="markdown-body">
                
                <h1 id="微信支付"><a href="#微信支付" class="headerlink" title="微信支付"></a>微信支付</h1><h2 id="JSAPI支付-公众号支付"><a href="#JSAPI支付-公众号支付" class="headerlink" title="JSAPI支付(公众号支付)"></a>JSAPI支付(公众号支付)</h2><p><strong>特点</strong>:</p>
<ul>
<li>用户通过消息或者扫描二维码再微信内部打开网页,可以调用微信支付完成下单购买的流程(公众号)</li>
<li>只能从微信浏览器发起,可以用微信浏览器的内置方法,不需要引用 Js-sdk</li>
</ul>
<p><strong>所需:</strong></p>
<ul>
<li>一个可以访问的域名(<a target="_blank" rel="noopener" href="http://www.xxx.com/">www.xxx.com</a>)</li>
<li>一个公众号</li>
</ul>
<h3 id="流程"><a href="#流程" class="headerlink" title="流程:"></a>流程:</h3><p><strong>步骤一</strong>:	添加开发者,微信授权登录配置</p>
<p><strong>添加开发者</strong>:</p>
<p>在公众号管理后台登录,选择开发者工具—-web开发者工具,  绑定开发者微信.</p>
<p><strong>微信授权登录配置:</strong></p>
<p>进入公众号后台管理平台,进入“开发 - 接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”</p>
<p>修改授权回调域名. 然后下载MP_verify文件至服务器根路径下</p>
<blockquote>
<p>注意点:</p>
<p>这里填写的是域名,不需要加http:&#x2F;&#x2F; 或者端口  也不允许出现ip地址</p>
</blockquote>
<p>​		</p>
<table>
<thead>
<tr>
<th>scope参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>snsapi_base</td>
<td>静默授权，获取openid，直接进入业务页面，用户无感知</td>
</tr>
<tr>
<td>snsapi_userinfo</td>
<td>需要用户手动同意，获取用户基本信息，对于已关注公众号的用户，如果用户从公众号的会话或者自定义菜单进入本公众号的网页授权页，即使是scope为snsapi_userinfo，也是静默授权，用户无感知</td>
</tr>
</tbody></table>
<p><strong>流程概括</strong></p>
<ol>
<li><p>引导用户进入授权页面同意授权，获取code，接口示例:</p>
<figure class="highlight pf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs pf"><br>https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&amp;redirect_uri=REDIRECT_URI&amp;response_type=code&amp;scope=SCOPE&amp;<span class="hljs-keyword">state</span>=STATE<span class="hljs-comment">#wechat_redirec</span><br></code></pre></td></tr></table></figure>

<ul>
<li>true_url：支付页面的实际地址</li>
<li>scope：如果仅需要用户的open_id，可以使用<code>snsapi_base</code>静默授权，否则使用<code>snsapi_userinfo</code>；</li>
<li>state：可选参数，可以带到实际网页中的参数，需要进行URI转码。希望获取<code>?order_id=123</code>，需要写成<code>state=$&#123;encodeURIComponent(&#39;order_id=123&#39;)&#125;</code>，实际页面拿到的字符为：<code>&amp;state=order_id%3D123</code>，然后需要在页面自行提取。</li>
<li>appid: 公众号id 可在公众号管理平台中查看</li>
</ul>
</li>
<li><p>在调用授权接口后通过解析url获取参数</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">//这里通过全局路由守卫来获取url参数</span><br>router.<span class="hljs-title function_">beforeEach</span>(<span class="hljs-function">(<span class="hljs-params">to,<span class="hljs-keyword">from</span>,next</span>)=&gt;</span>&#123;<br>    <span class="hljs-keyword">let</span> code = <span class="hljs-title class_">GetRequest</span>(<span class="hljs-string">&quot;code&quot;</span>) || <span class="hljs-string">&quot;&quot;</span>; <br>    <span class="hljs-keyword">let</span> appenid = <span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">getItem</span>(<span class="hljs-string">&quot;openId&quot;</span>) || <span class="hljs-string">&quot;&quot;</span>;	<span class="hljs-comment">// 有 openid 则不用再获取权限</span><br>    <span class="hljs-keyword">if</span>(!openid)&#123;<br>        <span class="hljs-keyword">if</span>(!code)&#123;<br>            <span class="hljs-keyword">const</span> path = <span class="hljs-built_in">encodeURIComponent</span>(<span class="hljs-string">&quot;http://www.baidu.com/mobile/index&quot;</span>);<br>            <span class="hljs-keyword">let</span> newURL = <span class="hljs-string">`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb339b3e2e964c144&amp;redirect_uri=<span class="hljs-subst">$&#123;path&#125;</span>&amp;response_type=code&amp;scope=snsapi_userinfo&amp;state=STATE#wechat_redirect`</span>;<br>            <span class="hljs-variable language_">window</span>.<span class="hljs-property">location</span>.<span class="hljs-title function_">replace</span>(newURL);<br>        &#125;<span class="hljs-keyword">else</span>&#123;<br>            <span class="hljs-comment">// 有 code 值，就做相应的操作。例如，登录需要携带 code ,返回具体的 openid,头像</span><br>        &#125;<br>    &#125;<span class="hljs-keyword">else</span>&#123;<br>        <span class="hljs-title function_">next</span>();<br>    &#125;<br>&#125;)<br><br><span class="hljs-comment">// 判断是否有 code , 有 code 的值就返回</span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">GetRequest</span>(<span class="hljs-params">queryName</span>)&#123;<br>  <span class="hljs-keyword">var</span> reg = <span class="hljs-keyword">new</span> <span class="hljs-title class_">RegExp</span>(<span class="hljs-string">&quot;(^|&amp;)&quot;</span> + queryName + <span class="hljs-string">&quot;=([^&amp;*])(&amp;|$)&quot;</span>,<span class="hljs-string">&quot;i&quot;</span>);<br>  <span class="hljs-keyword">var</span> i = <span class="hljs-variable language_">window</span>.<span class="hljs-property">location</span>.<span class="hljs-property">search</span>.<span class="hljs-title function_">substr</span>(<span class="hljs-number">1</span>).<span class="hljs-title function_">match</span>(reg);<br>  <span class="hljs-keyword">if</span>(r != <span class="hljs-literal">null</span>)&#123;<br>    <span class="hljs-keyword">return</span> <span class="hljs-built_in">decodeURI</span>(r[<span class="hljs-number">2</span>]);<br>  &#125;<span class="hljs-keyword">else</span>&#123;<br>    <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>


</li>
<li><p>通过code换取网页授权access_token（与基础支持中的access_token不同）</p>
</li>
<li><p>如果需要，开发者可以刷新网页授权access_token，避免过期</p>
</li>
<li><p>通过网页授权access_token和openid获取用户基本信息（支持UnionID机制）</p>
</li>
</ol>
<p>前端授权获取code后通过code调用后端接口获取用户信息，需要提供AppId(开发者id)，AppSecret(开发者密码)</p>
<p><strong>步骤二</strong>   :设置支付域名</p>
<p>前往微信商户平台设置</p>
<p><strong>步骤三</strong>  微信内调用支付可用微信内置方法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">//支付函数...</span><br><span class="hljs-title class_">PayOrder</span>(orderInfo, pageType) &#123;<br>    <span class="hljs-comment">//检测是否在微信环境中 否者支付方法无效</span><br>  <span class="hljs-keyword">if</span> (<span class="hljs-title function_">isWx</span>()) &#123;   <br>      <span class="hljs-title class_">Toast</span>.<span class="hljs-title function_">loading</span>(&#123;<br>            <span class="hljs-attr">message</span>: <span class="hljs-string">&quot;加载中...&quot;</span>,<br>            <span class="hljs-attr">forbidClick</span>: <span class="hljs-literal">true</span>,<br>          &#125;);<br>      <br>          <span class="hljs-title function_">orderPayOrder</span>(&#123; <span class="hljs-attr">id</span>: orderInfo.<span class="hljs-property">id</span> &#125;).<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">resOrder</span>) =&gt;</span> &#123;<br>            <span class="hljs-keyword">let</span> _this = <span class="hljs-variable language_">this</span>;<br>            <span class="hljs-keyword">var</span> <span class="hljs-title function_">onBridgeReady</span> = (<span class="hljs-params"></span>) =&gt; &#123;<br>              <span class="hljs-comment">// eslint-disable-next-line</span><br>               <span class="hljs-comment">//微信内置支付方法</span><br>              <span class="hljs-title class_">WeixinJSBridge</span>.<span class="hljs-title function_">invoke</span>(<br>                <span class="hljs-string">&quot;getBrandWCPayRequest&quot;</span>,<br>                &#123;<br>                  <span class="hljs-attr">appId</span>: resOrder.<span class="hljs-property">result</span>.<span class="hljs-property">appId</span>, <span class="hljs-comment">//公众号ID，此处必传</span><br>                  <span class="hljs-attr">timeStamp</span>: resOrder.<span class="hljs-property">result</span>.<span class="hljs-property">timestamp</span>, <span class="hljs-comment">//时间戳，timeStamp S需要大写</span><br>                  <span class="hljs-attr">nonceStr</span>: resOrder.<span class="hljs-property">result</span>.<span class="hljs-property">nonceStr</span>, <span class="hljs-comment">//随机串</span><br>                  <span class="hljs-attr">package</span>: resOrder.<span class="hljs-property">result</span>.<span class="hljs-property">payPackage</span>,<br>                  <span class="hljs-attr">signType</span>: resOrder.<span class="hljs-property">result</span>.<span class="hljs-property">signType</span>, <span class="hljs-comment">//微信签名方式MD5</span><br>                  <span class="hljs-attr">paySign</span>: resOrder.<span class="hljs-property">result</span>.<span class="hljs-property">paySign</span>, <span class="hljs-comment">//微信签名</span><br>                &#125;,<br>                <span class="hljs-keyword">function</span> (<span class="hljs-params">res</span>) &#123;<br>                  <span class="hljs-keyword">if</span> (res.<span class="hljs-property">err_msg</span> == <span class="hljs-string">&quot;get_brand_wcpay_request:ok&quot;</span>) &#123;<br>                    <span class="hljs-comment">// 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。</span><br>                    _this.<span class="hljs-title function_">refreshData</span>(orderInfo.<span class="hljs-property">id</span>, pageType);<br>                  &#125;<br>                  <span class="hljs-keyword">if</span> (res.<span class="hljs-property">err_msg</span> == <span class="hljs-string">&quot;get_brand_wcpay_request:fail&quot;</span>) &#123;<br>                    <span class="hljs-title class_">Toast</span>.<span class="hljs-title function_">fail</span>(<span class="hljs-string">&quot;支付失败&quot;</span>);<br>                    _this.<span class="hljs-title function_">refreshData</span>(orderInfo.<span class="hljs-property">id</span>, pageType);<br>                  &#125;<br>                  <span class="hljs-keyword">if</span> (res.<span class="hljs-property">err_msg</span> == <span class="hljs-string">&quot;get_brand_wcpay_request:cancel&quot;</span>) &#123;<br>                    <span class="hljs-title class_">Toast</span>.<span class="hljs-title function_">fail</span>(<span class="hljs-string">&quot;支付取消&quot;</span>);<br>                    _this.<span class="hljs-title function_">refreshData</span>(orderInfo.<span class="hljs-property">id</span>, pageType);<br>                  &#125;<br>                &#125;<br>              );<br>            &#125;;<br>            <span class="hljs-comment">//调用微信支付接口</span><br>            <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> <span class="hljs-title class_">WeixinJSBridge</span> == <span class="hljs-string">&quot;undefined&quot;</span>) &#123;<br>              <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">document</span>.<span class="hljs-property">addEventListener</span>) &#123;<br>                <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">addEventListener</span>(<br>                  <span class="hljs-string">&quot;WeixinJSBridgeReady&quot;</span>,<br>                  onBridgeReady,<br>                  <span class="hljs-literal">false</span><br>                );<br>              &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">document</span>.<span class="hljs-property">attachEvent</span>) &#123;<br>                <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">attachEvent</span>(<span class="hljs-string">&quot;WeixinJSBridgeReady&quot;</span>, onBridgeReady);<br>                <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">attachEvent</span>(<span class="hljs-string">&quot;onWeixinJSBridgeReady&quot;</span>, onBridgeReady);<br>              &#125;<br>            &#125; <span class="hljs-keyword">else</span> &#123;<br>              <span class="hljs-title function_">onBridgeReady</span>();<br>            &#125;<br>          &#125;);<br>        &#125; <span class="hljs-keyword">else</span> &#123;<br>            <span class="hljs-comment">//不在微信环境中的提示</span><br>          <span class="hljs-title class_">Dialog</span>.<span class="hljs-title function_">alert</span>(&#123;<br>            <span class="hljs-attr">message</span>: <span class="hljs-string">&quot;请在微信环境下打开，否则无法付款&quot;</span>,<br>            <span class="hljs-attr">className</span>: <span class="hljs-string">&quot;dialog-alert&quot;</span>,<br>          &#125;).<span class="hljs-title function_">then</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>            <span class="hljs-comment">// close</span><br>          &#125;);<br>        &#125;<br>  &#125;<br></code></pre></td></tr></table></figure>



<h2 id="JSSDK支付-chooseWXPay"><a href="#JSSDK支付-chooseWXPay" class="headerlink" title="JSSDK支付(chooseWXPay)"></a>JSSDK支付(chooseWXPay)</h2><h3 id="流程-1"><a href="#流程-1" class="headerlink" title="流程"></a>流程</h3><p><strong>步骤一</strong>  不同于jsapi支付  需要配置 <strong>js接口安全域名</strong></p>
<table>
<thead>
<tr>
<th><strong>参数名称</strong></th>
<th><strong>配置地址</strong></th>
<th><strong>意义</strong></th>
<th><strong>可配置个数</strong></th>
</tr>
</thead>
<tbody><tr>
<td>网页授权域名</td>
<td>公众号后台-公众号设置-功能设置</td>
<td>在获取code的接口中配置的redirect_uri必须在这个域名下</td>
<td>2</td>
</tr>
<tr>
<td>JS接口安全域名</td>
<td>公众号后台-公众号设置-功能设置</td>
<td>调用微信开放的JS接口的页面必须在此域名下</td>
<td>3</td>
</tr>
<tr>
<td>支付授权目录</td>
<td>产品中心-开发配置-支付授权目录</td>
<td>调起微信支付的页面所在的目录</td>
<td>5</td>
</tr>
</tbody></table>
<p><strong>步骤二</strong>  引入JS-SKD</p>
<p><strong>步骤三</strong>  通过config()方法注入权限验证配置</p>
<blockquote>
<p>同一个url仅需调用一次，对于变化url的SPA单页面的web app可在每次url变化时进行调用</p>
<p>在老版本对history.pushstate方法变化的路径不生效</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs javascript">wx.<span class="hljs-title function_">config</span>(&#123;<br>    <span class="hljs-attr">debug</span>: <span class="hljs-literal">true</span>, <span class="hljs-comment">// 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。</span><br>    <span class="hljs-attr">appId</span>: <span class="hljs-string">&#x27;&#x27;</span>, <span class="hljs-comment">// 必填，公众号的唯一标识</span><br>    <span class="hljs-attr">timestamp</span>: , <span class="hljs-comment">// 必填，生成签名的时间戳</span><br>    <span class="hljs-attr">nonceStr</span>: <span class="hljs-string">&#x27;&#x27;</span>, <span class="hljs-comment">// 必填，生成签名的随机串</span><br>    <span class="hljs-attr">signature</span>: <span class="hljs-string">&#x27;&#x27;</span>,<span class="hljs-comment">// 必填，签名</span><br>    <span class="hljs-attr">jsApiList</span>: [] <span class="hljs-comment">// 必填，需要使用的JS接口列表</span><br>&#125;);<br></code></pre></td></tr></table></figure>



<p><strong>步骤四</strong>  通过ready方法处理成功验证、通过error接口处理失败验证</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs javascript">wx.<span class="hljs-title function_">ready</span>(<span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) &#123;<br>    wx.<span class="hljs-title function_">checkJsApi</span>(&#123;<br>        <span class="hljs-attr">jsApiList</span>: [<span class="hljs-string">&#x27;chooseWXPay&#x27;</span>], <span class="hljs-comment">// 需要检测的JS接口列表，所有JS接口列表见附录2,</span><br>        <span class="hljs-attr">success</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) &#123;<br>        <span class="hljs-comment">// 以键值对的形式返回，可用的api值true，不可用为false</span><br>        <span class="hljs-comment">// 如：&#123;&quot;checkResult&quot;:&#123;&quot;chooseImage&quot;:true&#125;,&quot;errMsg&quot;:&quot;checkJsApi:ok&quot;&#125;</span><br>        &#125;<br>    &#125;);<br>&#125;);<br><br>wx.<span class="hljs-title function_">error</span>(<span class="hljs-keyword">function</span>(<span class="hljs-params">res</span>)&#123;<br>    <span class="hljs-comment">// config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开         config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。</span><br>&#125;);<br><br></code></pre></td></tr></table></figure>

<p><strong>注意</strong></p>
<p>config信息验证后会执行ready方法，所有接口调用都必须在config接口获得结果之后，config是一个客户端的异步操作，所以如果需要在页面加载时就调用相关接口，则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口，则可以直接调用，不需要放在ready函数中。</p>
<p><strong>步骤五</strong>   发起一个微信支付请求</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs javascript">wx.<span class="hljs-title function_">chooseWXPay</span>(&#123;<br>  <span class="hljs-attr">timestamp</span>: <span class="hljs-number">0</span>, <span class="hljs-comment">// 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符</span><br>  <span class="hljs-attr">nonceStr</span>: <span class="hljs-string">&#x27;&#x27;</span>, <span class="hljs-comment">// 支付签名随机串，不长于 32 位</span><br>  <span class="hljs-attr">package</span>: <span class="hljs-string">&#x27;&#x27;</span>, <span class="hljs-comment">// 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）</span><br>  <span class="hljs-attr">signType</span>: <span class="hljs-string">&#x27;&#x27;</span>, <span class="hljs-comment">// 签名方式，默认为&#x27;SHA1&#x27;，使用新版支付需传入&#x27;MD5&#x27;</span><br>  <span class="hljs-attr">paySign</span>: <span class="hljs-string">&#x27;&#x27;</span>, <span class="hljs-comment">// 支付签名</span><br>  <span class="hljs-attr">success</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params">res</span>) &#123;<br>    <span class="hljs-comment">// 支付成功后的回调函数</span><br>  &#125;<br>&#125;);<br><br><span class="hljs-comment">//参数通过统一下单接口拿到，paySign 采用统一的微信支付 Sign 签名生成方法，注意这里 appId 也要参与签名，appId 与 config 中传入的 appId 一致，即最后参与签名的参数有appId, timeStamp, nonceStr, package, signType。</span><br></code></pre></td></tr></table></figure>







<h2 id="h5微信支付"><a href="#h5微信支付" class="headerlink" title="h5微信支付"></a>h5微信支付</h2><p><strong>特点</strong>: </p>
<p>可以再微信客户端外的移动端网页支付. 再用户确认购买时唤起微信客户端进行支付.不能从微信客户端掉起</p>
<p><strong>申请支付功能</strong>: </p>
<p>登录商户平台–&gt;产品中心–&gt;我的产品–&gt;支付产品→H5支付</p>
<h1 id="响应式适配"><a href="#响应式适配" class="headerlink" title="响应式适配"></a>响应式适配</h1><h2 id="方案一-Rem-pxToRem插件"><a href="#方案一-Rem-pxToRem插件" class="headerlink" title="方案一: Rem + pxToRem插件"></a>方案一: Rem + pxToRem插件</h2><p><strong>原理:</strong></p>
<ol>
<li><p>监听屏幕视窗的宽度,通过一定比例换算赋值给html的<code>font-size</code>.此时的跟字体大小就会随着屏幕宽度变化而变化</p>
</li>
<li><p>将px转化成rem有两种可选方法</p>
<ol>
<li><p>利用<code>sass/less</code>中的自定义函数</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs css">$rootFontSize: <span class="hljs-number">375</span> / <span class="hljs-number">10</span>;<br>// 定义 px 转化为 rem 的函数<br><span class="hljs-keyword">@function</span> px2rem ($px) &#123;<br>    <span class="hljs-keyword">@return</span> $px / $rootFontSize + rem;<br>&#125;<br><br><span class="hljs-selector-class">.demo</span> &#123;<br>    <span class="hljs-attribute">width</span>: <span class="hljs-built_in">px2rem</span>(<span class="hljs-number">100</span>);<br>    <span class="hljs-attribute">height</span>: <span class="hljs-built_in">px2rem</span>(<span class="hljs-number">100</span>);<br>&#125;<br></code></pre></td></tr></table></figure>


</li>
<li><p>编译过程中利用webpack插件<code>postcss-pxtorem</code>全部转化完成</p>
</li>
</ol>
</li>
</ol>
<p><strong>实现:</strong></p>
<ul>
<li><p>监听屏幕宽度,实现动态根元素字体大小</p>
<ul>
<li><p>&#96;&#96;&#96;js<br>const MAX_FONT_SIZE &#x3D; 420</p>
<p>&#x2F;&#x2F; 定义最大的屏幕宽度<br>document.addEventListener(‘DOMContentLoaded’, () &#x3D;&gt; {<br>  const html &#x3D; document.querySelector(‘html’)<br>  let fontSize &#x3D; window.innerWidth &#x2F; 10<br>  fontSize &#x3D; fontSize &gt; MAX_FONT_SIZE ? MAX_FONT_SIZE : fontSize<br>  html.style.fontSize &#x3D; fontSize + ‘px’<br>})</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs php"><br>- 在vue-cli3中配置`postcss-pxtorem`插件<br><br>  - ```js<br>    <span class="hljs-keyword">const</span> <span class="hljs-variable constant_">autoprefixer</span> = <span class="hljs-keyword">require</span>(<span class="hljs-string">&#x27;autoprefixer&#x27;</span>)<br>    <span class="hljs-keyword">const</span> <span class="hljs-variable constant_">pxtorem</span> = <span class="hljs-keyword">require</span>(<span class="hljs-string">&#x27;postcss-pxtorem&#x27;</span>)<br>    module.exports = &#123; <br>      <span class="hljs-comment">// ...</span><br>      css: &#123;<br>        sourceMap: <span class="hljs-literal">true</span>,<br>        loaderOptions: &#123;<br>          postcss: &#123;<br>            plugins: [<br>              <span class="hljs-title function_ invoke__">autoprefixer</span>(),<br>              <span class="hljs-title function_ invoke__">pxtorem</span>(&#123;<br>                <span class="hljs-attr">rootValue</span>: <span class="hljs-number">37.5</span>,<br>                <span class="hljs-attr">propList</span>: [<span class="hljs-string">&#x27;*&#x27;</span>]<br>              &#125;)<br>            ]<br>          &#125;<br>        &#125;<br>      &#125;<br>    &#125;<br></code></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>移动端开发</div>
      <div>https://deng-zixiong.gitee.io/dzx-blog/2021/10/01/移动端开发/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2021年10月1日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/dzx-blog/2021/10/01/%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95/" title="开发记录">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">开发记录</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/dzx-blog/2021/09/07/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E4%B8%89%E5%A4%A7%E4%BB%B6/" title="前端基础三大件的学习">
                        <span class="hidden-mobile">前端基础三大件的学习</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/dzx-blog/js/events.js" ></script>
<script  src="/dzx-blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/dzx-blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/dzx-blog/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/dzx-blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
